package com.whfc.ms.api.mach.service.impl;

import com.whfc.common.file.FileHandler;
import com.whfc.common.file.FilePathConfig;
import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.common.util.RandomUtil;
import com.whfc.entity.dto.OssPathDTO;
import com.whfc.fuum.service.SysDeptService;
import com.whfc.mach.dto.AppMachDTO;
import com.whfc.mach.dto.maintain.MaintainDTO;
import com.whfc.mach.enums.MachState;
import com.whfc.mach.param.maintain.MachMaintainAddParam;
import com.whfc.mach.param.maintain.MachMaintainEditParam;
import com.whfc.mach.service.AppMachDataService;
import com.whfc.mach.service.AppMachMaintainService;
import com.whfc.ms.api.mach.service.MsMachMaintainService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.Method;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author sun_guodong
 * @date 2021-08-09
 */
@Service
public class MsMachMaintainServiceImpl implements MsMachMaintainService {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @DubboReference(interfaceClass = AppMachMaintainService.class, version = "1.0.0",
            methods = {@Method(name = "export", async = true, isReturn = false)})
    private AppMachMaintainService appMachMaintainService;

    @DubboReference(interfaceClass = AppMachDataService.class, version = "1.0.0")
    private AppMachDataService appMachDataService;

    @DubboReference(interfaceClass = SysDeptService.class, version = "1.0.0")
    private SysDeptService sysDeptService;

    @Autowired
    private FilePathConfig filePathConfig;

    @Autowired
    private FileHandler fileHandler;

    @Override
    public OssPathDTO upload(MultipartFile file) {
        String filename = file.getOriginalFilename();
        String suffix = "";
        if (StringUtils.isNotBlank(filename)) {
            suffix = filename.substring(filename.lastIndexOf("."));
        }
        String filePath = filePathConfig.getFilePath("mach/maintain/file");
        String fileName = RandomUtil.getRandomFileName() + suffix;
        String fileUrl = "";
        try {
            // 文件上传
            InputStream inputStream = file.getInputStream();
            fileUrl = fileHandler.upload(filePath + "/" + fileName, inputStream);
        } catch (Exception e) {
            logger.error("上传设备保养文件失败", e);
        }
        return new OssPathDTO(fileUrl);
    }

    @Override
    public Map<String, String> getConfig(String name) {
        String suffix = null;
        if (name != null) {
            suffix = name.substring(name.lastIndexOf("."));
        }
        String path = filePathConfig.getFilePath("mach/maintain/file");
        String newName = RandomUtil.getRandomFileName() + suffix;

        // 获取配置
        Map<String, String> policy = fileHandler.getPolicy(path);
        policy.put("fileName", newName);
        return policy;
    }

    @Override
    public PageData<MaintainDTO> list(Integer deptId, Integer pageNum, Integer pageSize, Date startDate, Date endDate, String keyword, Integer machType) {
        return appMachMaintainService.list(deptId, pageNum, pageSize, startDate, endDate, keyword, machType);
    }

    @Override
    public PageData<MaintainDTO> listByMachId(Integer machId, Integer pageNum, Integer pageSize) {
        return appMachMaintainService.listByMachId(machId, pageNum, pageSize);
    }

    @Override
    public MaintainDTO detail(Integer maintainId) {
        return appMachMaintainService.detail(maintainId);
    }

    @Override
    public void add(MachMaintainAddParam param) {
        appMachMaintainService.add(param);
    }

    @Override
    public void edit(MachMaintainEditParam param) {
        appMachMaintainService.edit(param);
    }

    @Override
    public void del(Integer maintainId) {
        appMachMaintainService.del(maintainId);
    }

    @Override
    public void export(Integer deptId, Date startDate, Date endDate, String keyword, Integer machType) {
        appMachMaintainService.export(deptId, startDate, endDate, keyword, machType);
    }

    @Override
    public ListData<AppMachDTO> getAvailableMach(Integer deptId, Integer machType) {
        List<Integer> deptIds = sysDeptService.getDescendantDeptIdList(deptId);
        return appMachDataService.list(deptIds, null, null, MachState.IN.getValue(), null, machType);
    }

}
